package com.work.biz.controller;

import java.util.List;

import com.work.mp.service.impl.MpTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.work.common.annotation.Log;
import com.work.common.core.controller.BaseController;
import com.work.common.core.domain.AjaxResult;
import com.work.common.enums.BusinessType;
import com.work.biz.domain.BizTeacher;
import com.work.biz.service.IBizTeacherService;
import com.work.common.utils.poi.ExcelUtil;
import com.work.common.core.page.TableDataInfo;

/**
 * 教师Controller
 *
 * @author lee
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/api/biz/teacher")
@Api(value = "教师api", tags = {"添加教师接口"})
public class BizTeacherController extends BaseController {
    @Autowired
    private IBizTeacherService bizTeacherService;

    @Autowired
    private MpTokenService tokenService;

    /**
     * 查询教师列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizTeacher bizTeacher) {
        startPage();
        List<BizTeacher> list = bizTeacherService.selectBizTeacherList(bizTeacher);
        return getDataTable(list);
    }

    /**
     * 导出教师列表
     */
    @Log(title = "教师", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizTeacher bizTeacher) {
        List<BizTeacher> list = bizTeacherService.selectBizTeacherList(bizTeacher);
        ExcelUtil<BizTeacher> util = new ExcelUtil<BizTeacher>(BizTeacher.class);
        return util.exportExcel(list, "教师数据");
    }

    /**
     * 获取教师详细信息
     */
    @GetMapping(value = "/{teacherId}")
    public AjaxResult getInfo(@PathVariable("teacherId") Long teacherId) {
        return AjaxResult.success(bizTeacherService.selectBizTeacherById(teacherId));
    }

    /**
     * 新增教师
     */
    @Log(title = "教师", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "指定教师", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult add(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "教师数据", required = true)
            @RequestBody BizTeacher bizTeacher
    ) {
        BizTeacher t = new BizTeacher();
        t.setUserId(bizTeacher.getUserId());
        t.setClassId(bizTeacher.getClassId());
        List<BizTeacher> list = bizTeacherService.selectBizTeacherList(t);
        if(list.size()>0) {
            return error("该用户已被指定为该班级教师");
        }else {
            bizTeacher.setTeacherId(tokenService.getUserId(tk));
            return toAjax(bizTeacherService.insertBizTeacher(bizTeacher));
        }
    }

    /**
     * 修改教师
     */
    @Log(title = "教师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizTeacher bizTeacher) {
        return toAjax(bizTeacherService.updateBizTeacher(bizTeacher));
    }

    /**
     * 删除教师
     */
    @Log(title = "教师", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teacherIds}")
    public AjaxResult remove(@PathVariable Long[] teacherIds) {
        return toAjax(bizTeacherService.deleteBizTeacherByIds(teacherIds));
    }
}
